package cn.tedu.study.exception;

import cn.tedu.study.response.ResultVO;
import cn.tedu.study.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.nio.file.AccessDeniedException;


/**
 * @author:yuziji
 * @create: 2023-06-08 14:39
 * @description:
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler({InternalAuthenticationServiceException.class, BadCredentialsException.class})
    public ResultVO handleAuthenticationException(AuthenticationException e) {
        if (e instanceof InternalAuthenticationServiceException) {
            log.warn("用户名不存在");
            return new ResultVO(StatusCode.USERNAME_ERROR);
        }
        log.warn("密码错误");
        return new ResultVO(StatusCode.PASSWORD_ERROR);
    }

    @ExceptionHandler(AccessDeniedException.class)
    public ResultVO handleAccessDeniedException(AccessDeniedException e) {
        log.warn("无权访问");
        return new ResultVO(StatusCode.FORBIDDEN_ERROR);
    }
}
